package com.example.bijidemo.Aop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.bijidemo.common.BaseContext;
import com.example.bijidemo.dto.LogDto;
import com.example.bijidemo.dto.RenwuDto;
import com.example.bijidemo.entity.*;
import com.example.bijidemo.mapper.RenWuMapper;
import com.example.bijidemo.service.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Aspect
@Component
public class XxxAspect {
    @Autowired
   private XxxService xxxService;
    @Autowired
   private RenWuService renWuService;
    @Autowired
    private RenwuWeiduService renwuWeiduService;
    @Autowired
    private RenwuJinchengService renwuJinchengService;
    @Autowired
    private LogService logService;
    @Autowired
    private CategoryService categoryService;


    @Pointcut("execution(* com.example.bijidemo.controller.RenwuController.update(..))")
    public void log() {
    }
    @Around("log()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();

        //获取用户修改前的任务的数据
        Object[] args = point.getArgs();
        RenwuDto rdt= (RenwuDto)args[0];
        Long id = rdt.getId();
        Renwu ren = renWuService.getById(id);
        //获取维度表中的数据
        LambdaQueryWrapper<RenwuWeidu> as=new LambdaQueryWrapper<>();
        as.eq(RenwuWeidu::getRenwuId,rdt.getId());
        List<RenwuWeidu> lists = renwuWeiduService.list(as);
        //根据任务id查询修改之前的任务状态
        LambdaQueryWrapper<RenwuJincheng> bs=new LambdaQueryWrapper<>();
        bs.eq(RenwuJincheng::getRenwuId,rdt.getId());
        RenwuJincheng renwuJinchengs = renwuJinchengService.getOne(bs);
        //根据id查询修改前的任务的分类
        LambdaQueryWrapper<Category> ad=new LambdaQueryWrapper<>();
        ad.eq(Category::getId,ren.getCategoryId());
        Category category = categoryService.getOne(ad);
        String categoryName = category.getName();

        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;




        //保存日志


        saveSysLog(point, time,ren,lists,renwuJinchengs,categoryName);
        return result;
    }
    @Transactional
    public void saveSysLog(ProceedingJoinPoint point, long time, Renwu ren, List<RenwuWeidu> lists,RenwuJincheng renwuJinchengs,String categoryName) {



        MethodSignature signature =  (MethodSignature)point.getSignature();
         xxx xx=new xxx();
        //获取哪个用户进行的操作的
        String name = BaseContext.getCurrentName();
        xx.setUsername(name);
        xx.setTime(time);
        //获取相关对象的信息
        String className = point.getTarget().getClass().getName();
        //获取相关方法的信息
        String methodName = signature.getName();
        xx.setMethod(className + "." + methodName + "()");
        Object[] args = point.getArgs();
        try{
            String params = Arrays.toString(args);
            xx.setParams(params);
        }catch (Exception e){

        }
        //获取创建时间
        xx.setCreateDate(new Date());

    //获取修改前的参数数据
       RenwuDto renwuDto= (RenwuDto)args[0];
       //把前面的数据先加入到xxx数据库的表中
        xxxService.save(xx);
        //根据任务的id查询一下增加后的任务维度表
        LambdaQueryWrapper<RenwuWeidu> a=new LambdaQueryWrapper<>();
        a.eq(RenwuWeidu::getRenwuId,renwuDto.getId());
        List<RenwuWeidu> list = renwuWeiduService.list(a);
        //根据任务id查询修改之后的任务状态
        LambdaQueryWrapper<RenwuJincheng> b=new LambdaQueryWrapper<>();
        b.eq(RenwuJincheng::getRenwuId,renwuDto.getId());
        RenwuJincheng renwuJincheng = renwuJinchengService.getOne(b);
        //根据dto的id获取修改后的renwu表中的数据的任务
        Renwu renwu = renWuService.getById(renwuDto.getId());
        //判断是不是修改了任务的名称 把任务的id与描述加入到log表中
        if (!ren.getName().equals(renwu.getName())){
            String name1 = renwu.getName();
            Log log=new Log();

            log.setOpeartion("修改了任务的名称成从："+ren.getName()+">>>"+name1);
           // log.setId();
            log.setXxxId(xx.getId());
            logService.save(log);
        }
        //判断是不是修改了任务的重要程度， 把任务的id以及优先级加入log 表中
        if (!ren.getYouxianji().equals(renwu.getYouxianji())){
            String youxianji = renwu.getYouxianji();
            Log log=new Log();

            log.setOpeartion("修改了任务的重要程度优先级从："+ren.getYouxianji()+">>>"+youxianji);
            log.setXxxId(xx.getId());
            logService.save(log);
        }
        if (!ren.getZhipairen().equals(renwu.getZhipairen())){
            String zhipairen = renwu.getZhipairen();
            Log log=new Log();

            log.setOpeartion("修改了指派人从："+ren.getZhipairen()+">>>"+zhipairen);
            log.setXxxId(xx.getId());
            logService.save(log);
        }
        if (!ren.getFuzeren().equals(renwu.getFuzeren())){
            String fuzeren = renwu.getFuzeren();
            Log log=new Log();

            log.setOpeartion("修改了负责人从:"+ren.getFuzeren()+">>>"+fuzeren);
            log.setXxxId(xx.getId());
            logService.save(log);
        }
        if (!ren.getImage().equals(renwu.getImage())){
            String image = renwu.getImage();
            Log log=new Log();

            log.setOpeartion("修改了照片从:"+ren.getImage()+">>>"+image);
            log.setXxxId(xx.getId());
            logService.save(log);
        }
        if (!ren.getCategoryId().equals(renwu.getCategoryId())){
            //找到修改之hou数据库项目的名称
            String categoryNamehou = renwuDto.getCategoryName();
            //之前
           String qian= categoryName;
            Log log=new Log();

            log.setOpeartion("修改了项目的分类从:"+qian+">>>"+categoryName);
            log.setXxxId(xx.getId());
            logService.save(log);
        }
        if (!ren.getDescription().equals(renwu.getDescription())){
            String description = renwu.getDescription();
            Log log=new Log();

            log.setOpeartion("修改了任务的描述从："+ren.getDescription()+">>>"+description);
            log.setXxxId(xx.getId());
            logService.save(log);
        }
        if (!ren.getJiezhishijian().equals(renwu.getJiezhishijian())){
            Date jiezhishijian = renwu.getJiezhishijian();
            Log log=new Log();

            log.setOpeartion("修改了截止时间从:"+ren.getJiezhishijian()+">>>"+jiezhishijian);
            log.setXxxId(xx.getId());
            logService.save(log);
        }
        if (!lists.equals(list)){



         //   log.setOpeartion("修改了任务的标签维度");
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < lists.size(); j++) {
                    if (!list.get(i).equals(lists.get(j))){
                        Log log=new Log();
                        log.setXxxId(xx.getId());
                        log.setOpeartion("修改了任务的标签维度从:"+lists.get(j).getName()+">>>"+list.get(i).getName());
                        logService.save(log);
                    }
                }
            }
            //log.setXxxId(xx.getId());
            //logService.save(log);
        }
        if (!renwuJinchengs.getValue().equals(renwuJincheng.getValue())){
            Log log=new Log();

            log.setOpeartion("修改了任务的状态从:"+renwuJinchengs.getValue()+">>>"+renwuJincheng.getValue());
            log.setXxxId(xx.getId());
            logService.save(log);
        }

    }

}
